<html>
<head><meta charset="utf-8"><title>ffi for [T; N] args · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html">ffi for [T; N] args</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198171106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198171106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198171106">(May 20 2020 at 09:02)</a>:</h4>
<p>Recently we had this <a href="https://github.com/rust-av/av-metrics/pull/94">PR</a> that produced something faulty, I reduced it to <a href="https://github.com/lu-zero/capi-testcase">this</a>, is it a cbindgen problem or something that should be addressed in rustc ?</p>



<a name="198171686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198171686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198171686">(May 20 2020 at 09:09)</a>:</h4>
<blockquote>
<p>it's kinda ugly, but such is C interop</p>
</blockquote>
<p><span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span> <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="198171962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198171962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198171962">(May 20 2020 at 09:12)</a>:</h4>
<p>I have few ideas on how to fix it on the cbindgen side (not accepting [T; 3] arguments when generating bindings), but now I'm wondering if there is something broken on the rustc side.</p>



<a name="198172083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198172083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198172083">(May 20 2020 at 09:13)</a>:</h4>
<p>Incidentally <code>f(a: *const [T; N])</code> on the rust side called as <code>T foo[N] = ...; f(foo)</code> on the C side, seems working ...</p>



<a name="198172331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198172331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198172331">(May 20 2020 at 09:16)</a>:</h4>
<p>I think you'll be more likely to find an answer by asking cbindgen folks what's the expected behaviour in cbindgen. I might be wrong though, I never did C bindings in rust <span aria-label="eyes" class="emoji emoji-1f440" role="img" title="eyes">:eyes:</span></p>



<a name="198172363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198172363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198172363">(May 20 2020 at 09:17)</a>:</h4>
<p>the code generated by cbindgen is faulty. That's sure.</p>



<a name="198172697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198172697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198172697">(May 20 2020 at 09:20)</a>:</h4>
<p>But then, what <code>extern "C" fn foo(my_array: [T; N])</code> is supposed to produce ^^; ?</p>



<a name="198174021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174021">(May 20 2020 at 09:34)</a>:</h4>
<p>I think this is a bug in the improper_ctypes lint: passing arrays by value has no C equivalent, so we should warn about it when an array is used as "top level" type. Similar to how passing PhantomData directly is considered "improper", but having PhantomData occur in repr(C) structs isn't.</p>



<a name="198174046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174046">(May 20 2020 at 09:34)</a>:</h4>
<p>I vaguely recall discussing this before</p>



<a name="198174127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174127">(May 20 2020 at 09:35)</a>:</h4>
<p>you can by having this map <code>[T; N]</code> -&gt; <code>typedef struct TN { T a[N] } TN;</code></p>



<a name="198174145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174145">(May 20 2020 at 09:35)</a>:</h4>
<p>Oh, I found it and it turns out someone fixed the bug I was thinking of and the lint <em>does</em> complain nowadays: <a href="https://github.com/rust-lang/rust/issues/66305">#66305</a></p>



<a name="198174213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174213">(May 20 2020 at 09:36)</a>:</h4>
<p>but not by default, I guess ^^</p>



<a name="198174337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174337">(May 20 2020 at 09:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="131070">Luca Barbato</span> <a href="#narrow/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args/near/198174127">said</a>:</p>
<blockquote>
<p>you can by having this map <code>[T; N]</code> -&gt; <code>typedef struct TN { T a[N] } TN;</code></p>
</blockquote>
<p>This is not passing an array by value, it's passing a struct by value. The Rust equivalent for that is a repr(C) struct containing an array.</p>



<a name="198174382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174382">(May 20 2020 at 09:37)</a>:</h4>
<p>C does not have arrays :P</p>



<a name="198174449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174449">(May 20 2020 at 09:38)</a>:</h4>
<p>array notation as argument is just sugar for the pointer notation ^^</p>



<a name="198174488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174488">(May 20 2020 at 09:38)</a>:</h4>
<p>I don't know what point you're trying to make.</p>



<a name="198174522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198174522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198174522">(May 20 2020 at 09:38)</a>:</h4>
<p>that it should be probably either an hard error or a default on lint</p>



<a name="198175078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198175078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198175078">(May 20 2020 at 09:44)</a>:</h4>
<p>Oh, right. The improper_ctypes lint <em>in general</em> (not just for by-value arrays) doesn't currently run on C-ABI functions <em>defined</em> in Rust, only in declarations from <code>extern {}</code> blocks. This is <a href="https://github.com/rust-lang/rust/issues/66220">https://github.com/rust-lang/rust/issues/66220</a></p>



<a name="198175440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198175440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luca Barbato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198175440">(May 20 2020 at 09:49)</a>:</h4>
<p>I see</p>



<a name="198269065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198269065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gary Guo <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198269065">(May 20 2020 at 23:14)</a>:</h4>
<blockquote>
<p>C does not have arrays</p>
</blockquote>
<p>That's an incorrect statement. C does have arrays, but it "decays" when it is used as a rvalue, and that's what happened in function parameters.</p>



<a name="198269092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198269092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gary Guo <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198269092">(May 20 2020 at 23:14)</a>:</h4>
<p>This is likely a cbindgen bug.</p>



<a name="198294751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198294751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198294751">(May 21 2020 at 07:49)</a>:</h4>
<p>Right, you can tell if something is an array or pointer via typeof.</p>



<a name="198294772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198294772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198294772">(May 21 2020 at 07:49)</a>:</h4>
<p>There's a macro in the Linux kernel that checks for arrayness.</p>



<a name="198709491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ffi%20for%20%5BT%3B%20N%5D%20args/near/198709491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ffi.20for.20.5BT.3B.20N.5D.20args.html#198709491">(May 25 2020 at 23:51)</a>:</h4>
<p>typeof? What is this gnu extension nonsense. sizeof however does work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>